package ui.prototypes;

import java.text.SimpleDateFormat;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

public class CashFlowCursorAdapter extends CursorAdapter {

    private final LayoutInflater mLayoutInflater;

    private final SimpleDateFormat mDateFormat;

    public CashFlowCursorAdapter(Context context, Cursor cursor) {
        super(context, cursor, true);

        mLayoutInflater = LayoutInflater.from(context);
        mDateFormat = new SimpleDateFormat(context.getString(R.string.date_format));
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        CashFlow cf = new CashFlow();
        CashFlow.readFromCursor(cursor, cf);

        TextView txtDesc  = (TextView)view.findViewById(R.id.txtItemDesc);
        txtDesc.setText(cf.getDescription());

        TextView txtDate  = (TextView)view.findViewById(R.id.txtItemDate);
        txtDate.setText(mDateFormat.format(cf.getDate().getTime()));

        TextView txtValue = (TextView)view.findViewById(R.id.txtItemValue);
        txtValue.setText(cf.getValue()+"");

        view.setTag(cf);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View v = mLayoutInflater.inflate(R.layout.list_item, parent, false);

        return v;
    }

}
